package com.supor.aiot.food.adapter;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.wireless.security.open.middletier.fc.IFCActionCallback;
import com.android.baseconfig.base.GlideApp;
import com.android.baseconfig.common.views.CircleImageView;
import com.android.baseconfig.common.views.RoundImageView;
import com.bumptech.glide.Glide;
import com.supor.aiot.common.app.AppAgent;
import com.supor.aiot.food.R;
import com.supor.aiot.food.bean.RecipeHotBean;
import com.supor.aiot.food.utils.DoubleClickUtils;

import java.util.List;

public class RecipeHotAdapter extends RecyclerView.Adapter<RecipeHotAdapter.ClassifyHolder> {
    private List<RecipeHotBean> mList;
    private Context mContext;
    private RecipeHotAdapter.OnClassifyClickListener recipeDeviceClick;
    private boolean showUnlike;
    private boolean showName;

    public void setUnlike(boolean unlike) {
        this.showUnlike = unlike;
    }

    public void setShowName(boolean showName) {
        this.showName = showName;
    }

    public RecipeHotAdapter(List<RecipeHotBean> mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }

    public void setRecipeHotListener(RecipeHotAdapter.OnClassifyClickListener listener) {
        this.recipeDeviceClick = listener;
    }

    private void TvOverFlowed(final TextView textView, RecipeHotAdapter.ClassifyHolder holder) {

//        ViewTreeObserver vto = textView.getViewTreeObserver();
//        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//            @SuppressWarnings("deprecation")
//            @Override
//            public void onGlobalLayout() {
//                textView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//                textView.getHeight();
//                double w0 = textView.getWidth();//控件宽度
//                double w1 = textView.getPaint().measureText(textView.getText().toString());//文本宽度
//                if (w1 >= w0) {
//                    holder.rlChefOuter.setVisibility(View.VISIBLE);
//                    holder.ivRecipeStar.setVisibility(View.VISIBLE);
//                    holder.rlAvatarOuter.setVisibility(View.GONE);
//                    holder.tvAvatarName.setVisibility(View.GONE);
//                    holder.ivRecipeMore.setVisibility(View.GONE);//需要换行就显示该控件
//                }
//            }
//        });
    }

    @NonNull
    @Override
    public RecipeHotAdapter.ClassifyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_recipe_hot, parent, false);
        return new RecipeHotAdapter.ClassifyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecipeHotAdapter.ClassifyHolder holder, int position) {
        RecipeHotBean bean = mList.get(position);
        holder.rlRecipeHotOuter.setOnClickListener(view -> recipeDeviceClick.OnItemClick(position));

        if (!TextUtils.isEmpty(bean.getRecipeType()) && bean.getRecipeType().equals("VIDEO")) {
            holder.ivPlayVideo.setVisibility(View.VISIBLE);
        } else {
            holder.ivPlayVideo.setVisibility(View.GONE);
        }

        if (AppAgent.isLogin() && bean.getFavoriteType() == 1) {
            holder.ivRecipeStar.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.stored_icon));
            holder.ivRecipeMore.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.stored_icon));
        } else {
            holder.ivRecipeStar.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.stored_icon_none));
            holder.ivRecipeMore.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.stored_icon_none));
        }

        holder.rlChefOuter.setOnClickListener(view -> {
            if (DoubleClickUtils.isFastDoubleClick())return;
            recipeDeviceClick.OnAvatarClick(position);
        });
        holder.ivRecipeStar.setOnClickListener(view -> {
            if (DoubleClickUtils.isFastDoubleClick())return;
            recipeDeviceClick.OnStarClick(position);});
        holder.ivRecipeMore.setOnClickListener(view -> {
            if (DoubleClickUtils.isFastDoubleClick())return;
            recipeDeviceClick.OnStarClick(position);});
        holder.ivRecipeStar.setOnClickListener(view -> {
            if (DoubleClickUtils.isFastDoubleClick())return;
            if (AppAgent.isLogin()) {
                if (bean.getFavoriteType() == 1) {
                    holder.ivRecipeStar.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.stored_icon_none));
                    holder.ivRecipeMore.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.stored_icon_none));
                } else {
                    holder.ivRecipeStar.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.stored_icon));
                    holder.ivRecipeMore.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.stored_icon));
                }
            }
            recipeDeviceClick.OnStarClick(position);
        });
        holder.ivRecipeMore.setOnClickListener(view -> {
            if (DoubleClickUtils.isFastDoubleClick())return;
            if (AppAgent.isLogin()) {
                if (bean.getFavoriteType() == 1) {
                    holder.ivRecipeStar.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.stored_icon_none));
                    holder.ivRecipeMore.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.stored_icon_none));
                } else {
                    holder.ivRecipeStar.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.stored_icon));
                    holder.ivRecipeMore.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.stored_icon));
                }
            }
            recipeDeviceClick.OnStarClick(position);
        });
        holder.rlUnlikeRecipe.setOnClickListener(view -> recipeDeviceClick.OnUnlikeRecipe(position));
        holder.rlUnlikeClassify.setOnClickListener(view -> recipeDeviceClick.OnUnlikeClassify(position));
        holder.rlUnlike.setOnClickListener(view -> holder.rlUnlike.setVisibility(View.GONE));

        GlideApp.with(mContext).load(bean.getImageVertical()).placeholder(new ColorDrawable(mContext.getResources().getColor(R.color.white_f5f5f5))).into(holder.ivRecipeImg);

        if (!TextUtils.isEmpty(bean.getRecipeName())) {
            holder.tvRecipeName.setText(bean.getRecipeName());
        }

        if (!TextUtils.isEmpty(bean.getChefIcon())) {
            holder.rlChefOuter.setVisibility(View.VISIBLE);
            holder.ivRecipeMore.setVisibility(View.GONE);
            holder.recipeAvatar.setVisibility(View.VISIBLE);
            holder.tvRecipeName.setVisibility(View.VISIBLE);
            Glide.with(mContext).load(bean.getChefIcon()).into(holder.recipeAvatar);
            if (!TextUtils.isEmpty(bean.getChefTitle()))holder.tvAvatarName.setText(bean.getChefTitle());
        } else {
            holder.rlChefOuter.setVisibility(View.GONE);
            holder.ivRecipeMore.setVisibility(View.VISIBLE);
            TvOverFlowed(holder.tvRecipeName, holder);
        }

        if (!TextUtils.isEmpty(bean.getCrmModelName())) {
            holder.tvRecipeDevice.setVisibility(View.VISIBLE);
            holder.tvRecipeDevice.setText(bean.getCrmModelName());
        } else {
            holder.tvRecipeDevice.setVisibility(View.GONE);
        }

//            if (!TextUtils.isEmpty(bean.getProductName())&&!TextUtils.isEmpty(bean.getModelId())){
//                holder.tvRecipeDevice.setVisibility(View.VISIBLE);
//                holder.tvRecipeDevice.setText(bean.getProductName()+bean.getModelId());
//            }else {
//                holder.tvRecipeDevice.setVisibility(View.GONE);
//            }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ClassifyHolder extends RecyclerView.ViewHolder {
        private CircleImageView recipeAvatar;
        private ImageView ivRecipeStar;
        private ImageView ivRecipeMore;
        private ImageView ivPlayVideo;
        private RoundImageView ivRecipeImg;
        private TextView tvAvatarName;
        private TextView tvRecipeName;
        private TextView tvRecipeDevice;
        private RelativeLayout rlRecipeHotOuter;
        private RelativeLayout rlUnlike;
        private RelativeLayout rlUnlikeRecipe;
        private RelativeLayout rlUnlikeClassify;
        private RelativeLayout rlChefOuter;
        private RelativeLayout rlAvatarOuter;

        public ClassifyHolder(@NonNull View itemView) {
            super(itemView);
            recipeAvatar = itemView.findViewById(R.id.iv_recipe_avatar);
            tvAvatarName = itemView.findViewById(R.id.tv_recipe_name);
            tvRecipeName = itemView.findViewById(R.id.tv_device_name);
            tvRecipeDevice = itemView.findViewById(R.id.tv_hot_device);
            rlRecipeHotOuter = itemView.findViewById(R.id.rl_recipe_hot_outer);
            ivRecipeStar = itemView.findViewById(R.id.iv_recipe_store);
            ivRecipeMore = itemView.findViewById(R.id.iv_recipe_more);
            rlUnlike = itemView.findViewById(R.id.rl_unlike);
            rlUnlikeRecipe = itemView.findViewById(R.id.rl_unlike_recipe);
            rlUnlikeClassify = itemView.findViewById(R.id.rl_unlike_classify);
            ivRecipeImg = itemView.findViewById(R.id.iv_recipe_hot);
            ivPlayVideo = itemView.findViewById(R.id.iv_play_vedio);
            rlChefOuter = itemView.findViewById(R.id.rl_chef_outer);
            rlAvatarOuter = itemView.findViewById(R.id.rl_avatar_outer);
        }
    }

    public interface OnClassifyClickListener {
        void OnItemClick(int position);

        void OnItemLongClick(int position);

        void OnAvatarClick(int position);

        void OnStarClick(int position);

        void OnUnlikeRecipe(int position);

        void OnUnlikeClassify(int position);
    }

}
